/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.service;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.dao.SignupConfigDao;
import com.boxin.ims.modules.wechat.entity.Lottery;
import com.boxin.ims.modules.wechat.entity.SignupConfig;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.BaseService;


/**
 * 连续签到设置
 * @author xiaoqian
 *
*	2014上午9:24:11
 */
@Component
@Transactional(readOnly = true)
public class SignupConfigService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SignupConfigService.class);
	
	@Autowired
	private SignupConfigDao signupConfigDao;
	
	@Autowired
	private WechatMemberService memberService;
	
	public SignupConfig get(Long id) {
		return signupConfigDao.findOne(id);
	}
	
	
	public List<SignupConfig> findAll(){
		DetachedCriteria dc = signupConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("delFlag", SignupConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		
		return signupConfigDao.find(dc);
	}
	
	public Page<SignupConfig> find(Page<SignupConfig> page, SignupConfig SignupConfig) {
		DetachedCriteria dc = signupConfigDao.createDetachedCriteria();
		dc.createAlias("user", "user");
		if (SignupConfig.getUser()!=null &&SignupConfig.getUser().getId()!=null&& SignupConfig.getUser().getId()>0){
			dc.add(Restrictions.eq("user.id", SignupConfig.getUser().getId()));
		}
		if (SignupConfig.getUser()!=null &&SignupConfig.getUser().getName()!=null){
			dc.add(Restrictions.eq("user.name", SignupConfig.getUser().getName()));
		}
		if (StringUtils.isNotEmpty(SignupConfig.getRemarks())){
			dc.add(Restrictions.like("remarks", "%"+SignupConfig.getRemarks()+"%"));
		}
		dc.add(Restrictions.eq("delFlag", SignupConfig.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return signupConfigDao.find(page, dc);
	}
	
	public SignupConfig getByUserId(Long userId){
		
		DetachedCriteria dc = signupConfigDao.createDetachedCriteria();
		dc.add(Restrictions.eq("user.id", userId));
		
		List<SignupConfig> result = signupConfigDao.find( dc);
		return (result == null || result.isEmpty()) ? null : result.get(0);
	}
	
	@Transactional(readOnly = false)
	public void save(SignupConfig SignupConfig) {
		signupConfigDao.save(SignupConfig);
	}
	
	@Transactional(readOnly = false)
	public void delete(Long id) {
		signupConfigDao.deleteById(id);
	}
	
	/**
	 * @author  xiaoqian
	 * @time 2014-3-12  下午	14:07:17
	 */
	
	public Integer getTodayScore(UserDefine userDefine,WechatMember member,SignupConfig signupConfig){
		Integer score = 0;
		if(signupConfig.getStartup()&&signupConfig!=null){
			if(signupConfig.getDayNumber()==1){
				score=userDefine.getSignScore()+((member.getSignupCount()-1)/signupConfig.getDayNumber())*signupConfig.getScore();
			}else{
				score=userDefine.getSignScore()+((member.getSignupCount())/signupConfig.getDayNumber())*signupConfig.getScore();
				//今日签到积分=默认积分+（（签到次数-1）/签到周期）*奖励积分
				System.out.println("member.getSignupCount():"+member.getSignupCount());
				System.out.println("signupConfig.getDayNumber():"+signupConfig.getDayNumber());
				System.out.println("member.getSignupCount()%signupConfig.getDayNumber():"+member.getSignupCount()/signupConfig.getDayNumber());
				System.out.println("signupConfig.getScore():"+signupConfig.getScore());
				System.out.println("score:"+score);
			}
			
			
				
			if(signupConfig.getMaxScore()!=null){
				if(!(signupConfig.getMaxScore()==0)){
					if(score>=signupConfig.getMaxScore()){
						score=signupConfig.getMaxScore();
					}
				}
			}
			
		}else{
			score = (score == null) ? 1 :  userDefine.getSignScore();
		}
		
		return score;
	}
	
	public void isSignup(SignupConfig signupConfig,WechatMember member){
		if(signupConfig!=null  && signupConfig.getStartup()){
			//判断是否连续签到了
			if(StringUtils.isNotEmpty(member.getLastSignTime())){
				if((Integer.parseInt(DateUtils.getTodayString())-Integer.parseInt(member.getLastSignTime()))==1){
					if(member.getSignupCount()==null){
						member.setSignupCount(1);
					}else{
						member.setSignupCount(member.getSignupCount()+1);
					}
				}else{
					member.setSignupCount(1);
				}
			}else{
				member.setSignupCount(1);
			}
		}else{
			member.setSignupCount(0);
		}
	}
	
}
